package com.ljt.system.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljt.response.Result;
import com.ljt.system.entity.User;
import com.ljt.system.service.UserService;
import com.ljt.system.service.UserVoService;
import com.ljt.system.vo.UserVo;
import com.ljt.utils.ExcelUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author ljt
 * @since 2021-06-27
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private UserVoService userVoService;

    @ApiOperation(value = "用户列表", notes = "模糊查询用户信息")
    @GetMapping("/queryAllUserByUserName")
    public Result queryAllUserByUserName(@RequestParam(required = false,defaultValue = "1") Integer current,
                                     @RequestParam(required = false,defaultValue = "6") Integer size,@RequestParam(required=false) Integer departmentId,@RequestParam String username) {

        Page<UserVo> userPage = new Page<>(current, size);
        userPage = userVoService.queryUserByName(userPage,departmentId,username);
        return Result.ok().data("userPage", userPage);
    }

    @ApiOperation(value = "用户列表", notes = "查询所有的用户信息")
    @GetMapping("/login")
    public Result queryAllUserByPage() {
        return Result.ok();
    }

    @ApiOperation(value = "用户信息", notes = "根据id删除用户信息")
    @GetMapping("/deleteUserById")
    public Result deleteUserById(int id){
        boolean i=userService.removeById(id);
        if(i){
            return Result.ok();
        }else{
            return Result.error();
        }
    }
    @ApiOperation(value = "用户信息", notes = "根据id修改用户信息")
    @PostMapping("/updateUserById")
    public Result updateUserById(@RequestBody User user){
        System.out.println(user);
        Date date=new Date();
        user.setModifiedTime(date);
        boolean i=userService.updateById(user);
        if(i){
            return Result.ok().data("user",user);
        }else{
            return Result.error();
        }
    }
    @ApiOperation(value = "用户信息", notes = "添加/注册用户")
    @PostMapping("/insertUser")
    public Result insertUser(@RequestBody User user){
        System.out.println(user);
        boolean i=userService.save(user);
        if(i){
            return Result.ok().data("user",user);
        }else{
            return Result.error();
        }
    }

    @ApiOperation(value = "导出",produces="application/octet-stream")
    @GetMapping("/exportUserToExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        List<User> userData = userService.list();
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        util.exportExcel(response,userData , "用户数据");
    }

    @ApiOperation("导入")
    @PostMapping("/importUserToExcel")
    public Result importExcel(@RequestBody MultipartFile file) throws Exception {
        ExcelUtil<User> util = new ExcelUtil<>(User.class);
        List<User> user = util.importExcel(file.getInputStream());
        boolean insertFlag = userService.saveBatch(user);
        if (insertFlag){
            return Result.ok();
        }else{
            return Result.error();
        }
    }




    @ApiOperation(value = "用户信息", notes = "根据id修改用户信息")
    @GetMapping("/updateUserById2")
    public Result updateUserById2( User user){
        System.out.println(user);
        boolean i=userService.updateById(user);
        if(i){
            return Result.ok().data("user",user);
        }else{
            return Result.error();
        }
    }
}

